<template>
  <div>
    <el-form :model="nodeModalForm" ref="nodeModal" label-placement="left" label-width="100px" :rules="nodeModalRules"
             size="small">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="节点名称" prop="nodeName">
            <el-input v-model.trim="nodeModalForm.nodeName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <table border="1" align="center" class="add_table w_100" cellspacing="0">
      <tr>
        <th width="70"><span class="iconfont icon-Add cursor_point" @click="handleAddFilter"></span></th>
        <th>字段</th>
        <th>条件</th>
        <th>值</th>
      </tr>
      <tr v-for="(item,index) in nodeModalForm.filter" :key="index">
        <td width="70">
          <span class="iconfont icon-Add cursor_point px_2" @click="handleAddFilter(index)"></span>
          <span class="iconfont icon-close cursor_point px_2" @click="handleDeleteFilter(index)"></span>
        </td>
        <td>
          <el-select v-model="item.field" @change="fieldAction(item,index)" clearable filterable placeholder="字段"
                     class="fixed-icon w_100">
            <el-option v-for="item in nodeModalForm.fields" :key="item.alias" :label="item.alias" :value="item.alias">
              <span style="float: left">{{ item.alias }}</span>
              <span style="float: right;color: var(--el-text-color-secondary);font-size: 13px;">{{
                  item.DATA_TYPE
                }}</span>
            </el-option>
          </el-select>
        </td>
        <td>
          <el-select v-model="item.function" clearable filterable placeholder="条件" class="fixed-icon w_100">
            <el-option v-for="item in functionList" :key="item.value" :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </td>
        <td width="30%">
          <template v-if="!functionAction(item)">
            <el-input v-if="fieldType(item.DATA_TYPE) === 'input'" v-model.trim="item.val"
                      :disabled="functionAction(item)"></el-input>
            <el-input-number v-else-if="fieldType(item.DATA_TYPE) === 'number'" v-model.number="item.val"
                             :disabled="functionAction(item)"></el-input-number>
            <el-date-picker v-else-if="fieldType(item.DATA_TYPE) === 'date'" v-model="item.val" type="datetime"
                            :default-time="defaultTime"></el-date-picker>
          </template>
        </td>
      </tr>
    </table>
    <div class="nodeModal-footer">
      <span class="dialog-footer">
        <el-button plain size="small" @click="handleConfigCancel">取消</el-button>
        <el-button type="primary" size="small" @click="handleConfigConfirm">更新</el-button>
      </span>
    </div>
  </div>
</template>

<script>
import { computed, defineComponent, inject, onMounted, ref } from "vue"
import { functionList } from "../../../../../utils/dict"
import { useEtlStore } from "../../../../../store/flowEtl"
import { charType, dateType, numberType } from "../../../../../utils/constant"

export default defineComponent({
  name: "node-config-filter",
  props: {
    node: Object,
  },
  components: {},
  setup (props, ctx) {
    const etlStore = useEtlStore()
    const nodeList = computed(() => etlStore.nodeList)
    const nodeConfigClose = inject("nodeConfigClose")
    const defaultTime = new Date(2000, 1, 1, 12, 0, 0)
    const nodeModalForm = ref({
      nodeName: "",
      fields: [],
      filter: []
    })
    const nodeModal = ref()
    const nodeModalRules = ref({
      nodeName: {
        required: true,
        message: "请输入数据源名称",
        trigger: "blur",
      },
    })
    const fieldType = (type) => {
      if (!type) {
        return "";
      }
      let regType = type.replace(/[0-9]+/g, "")
      if (numberType.includes(regType)) {
        return "number"
      } else if (dateType.includes(regType)) {
        return "date"
      } else if (charType.includes(regType)) {
        return "input"
      } else {
        return ""
      }
    }
    const handleConfigCancel = () => {
      nodeConfigClose()
    }
    const nodeConfigUpdate = (node) => {
      let nodes = [...nodeList.value].map((item) => {
        if (item.id === node.id) {
          item.status = true
          item.nodeName = node.nodeName
          item.config.filter = node.filter
        }
        return item
      })
      etlStore.setNodeList(nodes)
      nodeConfigClose()
    }
    const handleConfigConfirm = () => {
      nodeModal.value.validate((valid) => {
        if (valid) {
          nodeConfigUpdate(nodeModalForm.value)
        } else {
          return false
        }
      })
    }
    const resetForm = () => {
      nodeModalForm.value = {
        id: null,
        nodeName: "",
        filter: [],
        fields: []
      }
    }
    const handleAddFilter = (index) => {
      if (index) {
        nodeModalForm.value.filter = [...nodeModalForm.value.filter.splice(0, index + 1), {
          field: "",
          function: "",
          val: "",
        }, ...nodeModalForm.value.filter.splice(index - 1, nodeModalForm.value.filter.length)]
      } else {
        nodeModalForm.value.filter.push({
          field: "",
          function: "",
          val: "",
        })
      }
    }
    const handleDeleteFilter = (index) => {
      nodeModalForm.value.filter.splice(index, 1)
    }
    const functionAction = (item) => {
      if (["empty", "not empty", "null", "notNull"].includes(item.function)) {
        return true
      } else {
        item.value = ""
        return false
      }
    }
    const fieldAction = (item, idx) => {
      let row = nodeModalForm.value.fields.find((row) => {
        return item.field === row.alias
      })
      nodeModalForm.value.filter[idx].DATA_TYPE = row.DATA_TYPE
    }
    onMounted(() => {
      resetForm()
      nodeModalForm.value = {
        id: props.node.id,
        nodeName: props.node.nodeName,
        fields: props.node.config.fields,
        filter: props.node.config.filter || []
      }
    })
    return {
      nodeModalForm,
      nodeModalRules,
      nodeModal,
      defaultTime,
      nodeList,
      functionList,
      resetForm,
      fieldType,
      handleConfigCancel,
      handleConfigConfirm,
      nodeConfigUpdate,
      nodeConfigClose,
      handleAddFilter,
      handleDeleteFilter,
      functionAction,
      fieldAction,
    }
  },
})
</script>

<style lang="less" scoped>
.nodeModal-footer {
  padding: var(--el-dialog-padding-primary);
  padding-top: 10px;
  text-align: right;
  box-sizing: border-box;
}

.fixed-icon {
  ::v-deep .el-icon svg {
    height: 100%;
  }
}
</style>